package com.konne.nightmare.FastPublicOpinion.mvp.presenter;


import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.base.OnLoadDataListener;
import com.konne.nightmare.FastPublicOpinion.bean.CurrencyBean;
import com.konne.nightmare.FastPublicOpinion.bean.LoginBean;
import com.konne.nightmare.FastPublicOpinion.http.BaseResponse;
import com.konne.nightmare.FastPublicOpinion.http.RetrofitFactory;
import com.konne.nightmare.FastPublicOpinion.mvp.model.ILoginActivityModel;
import com.konne.nightmare.FastPublicOpinion.mvp.model.IMainActivityModel;
import com.konne.nightmare.FastPublicOpinion.mvp.model.impl.LoginActivityModelImpl;
import com.konne.nightmare.FastPublicOpinion.mvp.model.impl.MainActivityModelImpl;
import com.konne.nightmare.FastPublicOpinion.mvp.view.ILoginView;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IMainView;
import com.konne.nightmare.FastPublicOpinion.utils.MMKVUtils;
import com.konne.nightmare.FastPublicOpinion.utils.Utils;

import org.json.JSONObject;

import java.util.List;


public class LoginPresenter extends BaseMvpPresenter<ILoginView> {
    private ILoginActivityModel iLoginActivityModel;

    public LoginPresenter() {
        iLoginActivityModel = new LoginActivityModelImpl();
    }

    public void login() {
        if (mView == null) return;
        JSONObject params = RetrofitFactory.getJsonParams();
        String mmkvString = MMKVUtils.getMMKVString(Utils.DEVICE_ID);
        try {
            params.put("msg", mView.msg());
            params.put("phone", mView.phoneNumber());
            params.put("phoneCode", mmkvString);
        } catch (Exception e) {

        }
        iLoginActivityModel.login(params, new OnLoadDataListener<LoginBean.DataBean>() {
            @Override
            public void onSuccess(BaseResponse<LoginBean.DataBean> t) {
                getView().login(t);
                getView().cancelLoadDialog();
            }

            @Override
            public void onFailure(String error) {
                getView().cancelLoadDialog();
            }
        });
    }

    public void getPhoneActivationMsg() {
        if (mView == null) return;
        JSONObject params = RetrofitFactory.getJsonParams();
        try {
            params.put("phone", mView.phoneNumber());
        } catch (Exception e) {

        }
        iLoginActivityModel.getPhoneActivationMsg(params, new OnLoadDataListener<CurrencyBean>() {
            @Override
            public void onSuccess(BaseResponse<CurrencyBean> t) {
                getView().getPhoneActivationMsg(t);
                getView().cancelLoadDialog();
            }

            @Override
            public void onFailure(String error) {
                getView().cancelLoadDialog();
            }
        });
    }

}
